package com.ruoyi.system.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class cutPicture {

    public static void main(String[] args){
//        try {
//            cut(100, 200, 200, 300,"D:\\BME\\iBrain\\png\\58\\base_588.png");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("图片截取成功！");
    }
    public static void cut(int x1,int y1,int x2,int y2,String url1,String url2) throws IOException {
        // 读取原始图片
        BufferedImage image = ImageIO.read(new File(url1));


        // 计算矩形宽度和高度
        int width = x2 - x1;
        int height = y2 - y1;

        // 创建新的BufferedImage对象，用于存储截取的矩形区域
        BufferedImage croppedImage = new BufferedImage(width, height, image.getType());

        // 截取矩形区域
        Graphics2D g = croppedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, x1, y1, x2, y2, null);

        // 保存截取的图片
        ImageIO.write(croppedImage, "png", new File(url2));

        System.out.println("图片截取成功！");
    }
}
